package com.zp.self.module.spring常用.国际化;

import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service;

import java.util.Locale;

/**
 * @author ：byH
 * @date ：Created 2022/3/27 15:40
 * @description：i18n（其来源是英文单词 internationalization的首末字符i和n，18为中间的字符数）是“国际化”的简称
 */
@Service
public class I18nService {

    private final MessageSource messageSource;
    private final MessageSource exceptionMessageSource;

    public I18nService(MessageSource messageSource, MessageSource exceptionMessageSource) {
        this.messageSource = messageSource;
        this.exceptionMessageSource = exceptionMessageSource;
    }

    public String getMessage(String msgKey) {
        return getMessage(msgKey,null);
    }
    public String getMessage(String msgKey, Object[] args) {
        return messageSource.getMessage(msgKey, args,"", Locale.forLanguageTag("en"));
    }

    public String getExceptionMessage(String msgKey) {
        return getExceptionMessage(msgKey,null);
    }

    public String getExceptionMessage(String msgKey, Object[] args) {
        return exceptionMessageSource.getMessage(msgKey, args, LocaleContextHolder.getLocale());
    }

}
